/*
 *  Copyright 2010 mazhao.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package shopping.management.onthefly;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import shopping.commons.BaseAction;
import shopping.management.ProductAction;

/**
 *
 * @author mazhao
 */
@org.apache.struts2.convention.annotation.Namespace("/management/onthefly")
@org.apache.struts2.convention.annotation.ParentPackage(value="shopping-json-default")
@org.apache.struts2.convention.annotation.InterceptorRefs({
    @org.apache.struts2.convention.annotation.InterceptorRef("jsonValidationWorkflowStack"),
    @org.apache.struts2.convention.annotation.InterceptorRef("defaultStack")
})
public class ProductImageAction extends BaseAction{
    private Log log = LogFactory.getLog(this.getClass());

    //----------------------------------------------
    // properties for list
    //----------------------------------------------
    private int productId;
    private List<String> images = new ArrayList<String>();

    public List<String> getImages() {
        return images;
    }

    public void setImages(List<String> images) {
        this.images = images;
    }

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    @Action(value="/management/onthefly/listimages",
        results={
            @Result(name="success", type="json")
        }
    )
    public String listImages() {
        if(log.isDebugEnabled()) {
            log.debug("enter management onthe fly listimages action");
            log.debug("productId:" + productId);
        }
        String uploadDir = getText(ProductAction.KEY_PRODUCT_IMAGE_DIR);
        File baseDir = new File(uploadDir + "/" + productId);

        log.debug("base dir:" + baseDir.getAbsolutePath());

        String[] files = baseDir.list(new FilenameFilter(){

            public boolean accept(File dir, String name) {
                //@todo: add all supported image types
                return true;
            }
        });

        if(images == null) {
            images = new ArrayList<String>();
        }
        images.clear();
        log.debug("after initialization of list");

        for(String file : files) {
            images.add("/" + productId + "/" + file);
        }

        log.debug("image list:" + images.size());
        
        return SUCCESS;
    }

    //----------------------------------------------
    // properties for delete
    //----------------------------------------------

    private String path;

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    @Action(value="/management/onthefly/deleteimage",
        results={
            @Result(name="success", type="json")
        }
    )
    public String deleteImage() {

        log.debug("enter delete image action");
        log.debug("delete image path:" + path);
        File toBeDel = new File(getText(ProductAction.KEY_PRODUCT_IMAGE_DIR) + "/" +  path);
        if(toBeDel.exists()) {
            if(!toBeDel.delete()){
                log.error("Can not delete file for product " + productId + " with file name " + path);
            }
        } else {
            log.warn("Can not delete file for product " + productId + " with file name " + path);
        }
        return SUCCESS;
    }
}
